/*年轻人，只管向前看，不要管自暴自弃者的话*/
package com.composetest.page.product

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.composetest.R
import com.composetest.core.model.Product
import com.composetest.ui.theme.Rounded5
import com.composetest.ui.theme.Rounded7
import com.composetest.ui.theme.SpacerH5
import com.composetest.ui.theme.SpacerW5
import com.composetest.util.ResourceUtil

/**
 * create by itz on 2024-10-14 11:12
 * desc :
 **/

@Composable
fun ItemProduct(data: Product, modifier: Modifier = Modifier): Unit {
    Row(
        modifier = modifier
            .fillMaxWidth()
            .clip(Rounded5)
            .background(Color.White)
            .padding(7.dp)
    ) {
        if (data.icon != null)
            AsyncImage(
                model = ResourceUtil.url(data.icon),
                placeholder = painterResource(id = R.drawable.ic_launcher_background),
                error = painterResource(id = R.drawable.ic_launcher_background),
                contentDescription = null,
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(135.dp)
                    .clip(Rounded7)
                    .background(Color.LightGray)
            )
        else
            Image(
                painter = painterResource(id = R.drawable.ic_launcher_background),
                contentDescription = null,
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(135.dp)
                    .clip(Rounded7)
                    .background(Color.LightGray)
            )
        Column(
            modifier = Modifier
                .weight(1f)
                .padding(10.dp)
        ) {
            Text(
                text = data.title,
                maxLines = 2,
                overflow = TextOverflow.Ellipsis,
                style = MaterialTheme.typography.titleSmall,
                color = MaterialTheme.colorScheme.onSurface
            )
            SpacerH5()
            Text(
                text = data.highlight ?: "",
                style = MaterialTheme.typography.bodySmall,
                color = MaterialTheme.colorScheme.onSurface
            )
            SpacerH5()
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(
                    text = "价格：${data.price}",
                    style = MaterialTheme.typography.titleMedium,
                    color = MaterialTheme.colorScheme.primary
                )

                SpacerW5()

                Text(
                    text = "${data.originPrice}",
                    style = MaterialTheme.typography.bodySmall.copy(
                        textDecoration = TextDecoration.LineThrough
                    ),
                    color = MaterialTheme.colorScheme.outline
                )
            }
            SpacerH5()
            Text(
                text = "评价 ${data.commentsCount}",
                style = MaterialTheme.typography.bodySmall,
                color = MaterialTheme.colorScheme.outline
            )
        }
    }
}

@Preview
@Composable
fun ItemProductPreview(): Unit {
    ItemProduct(
        data = Product(
            id = "1",
            title = "TCL 408升十字对开双开四开门智能一级能效风冷无霜大容量",
            price = 100,
            originPrice = 199,
            sale = 1,
            stocksCount = 2,
            commentsCount = 99
        )
    )
}